home *** CD-ROM | disk | FTP | other *** search
- /*
- PixelFlippercdev.c
- written in Think C 4.0 by Chris Sanchez
- Copyright ©1990 Chris Sanchez, All Rights Reserved
- */
-
- #include "PixelFlipper.h"
- #include "FlipperUtils.h"
- #include <cdev.h>
-
- /* PixelFlipper cdev subclass */
- struct PixelFlipper:cdev{
-
- /* Instance variables */
- PREFPtr fPrefs, fOldPrefs; /* Preferences record pointer. original and real */
- Boolean fPrefsCreat, fAbout; /* prefs were created, doing about */
- short fVRef, fLoaded, fScrolled, fAmount; /* working directory of the system folder, was INIT loaded, amount scrolled */
- PicHandle fAboutPic; /* handle to the about picture */
- RgnHandle fClipRgn; /* this is a clipping region, so we dont have to keep on cretine new ones */
- Cursor fCursor;
-
- /* PixelFlipper Methods */
- void Init();
- void Close();
- void Update();
- void ItemHit();
- void Idle();
- void DoCursor();
- void Toggle(short whichKey, ControlHandle theItem);
- void SetDCtl(short which, char value);
- void DrawAbout();
- void SetPrefsCtl();
- void ClearPrefsCtl();
- };
-
-
- Boolean Runnable()
- {
- return(TRUE);
- }
-
-
- cdev *New()
- {
- return new(PixelFlipper);
- }
-
- void PixelFlipper::Init()
- { SysEnvRec s;
- PREFPtr tPrefs;
- CursHandle cursH;
- Rect bounds;
- short theType;
- Handle theItem;
- DialogPtr theDlog;
-
- SysEnvirons (curSysEnvVers,&s); /* Get some system info. */
- fVRef = s.sysVRefNum;
- fPrefsCreat = fAbout = FALSE;
- fLoaded = fScrolled = 0;
- fClipRgn = NewRgn();
- fAboutPic = GetPicture(kCdevAbout);
- MoveHHi(fAboutPic);
- theDlog = dp;
- GetDItem(theDlog, kB1 + lastItem, &theType, &theItem, &bounds);
- fAmount = (**fAboutPic).picFrame.bottom - (**fAboutPic).picFrame.top - (bounds.bottom - bounds.top);
- cursH = GetCursor(kCursID);
- fCursor = **cursH;
- ReleaseResource(cursH);
-
- fPrefs = (PREFPtr)FindInitData();
-
- if (fPrefs == NIL){ /* can't find the fPrefs pointer from the init */
- fLoaded = kNotLoaded;
- if ((fPrefs = (PREFPtr)NewPtrClear(sizeof(PREFRec))) == NIL)
- return;
- tPrefs = fPrefs;
- if (!Get_Set_Prefs(tPrefs, s.sysVRefNum, getting)) /* Try to get the preferences */
- return;
- }
- if ((fOldPrefs = (PREFPtr)NewPtrClear(sizeof(PREFRec))) == NIL)
- fOldPrefs = fPrefs; /* just make it point to the old data */
- else{ /* we were able to create the fOldPrefs rec */
- fPrefsCreat = TRUE;
- *fOldPrefs = *fPrefs; /* copy the data to the old fPrefs */
- }
- SetPrefsCtl();
- Update();
- inherited::Init();
- }
-
- void PixelFlipper::Close()
- { PREFPtr tempPrefs;
- short theRef;
- OSErr err;
- PicHandle pic;
-
- if (fPrefsCreat){
- *fPrefs = *fOldPrefs;
- tempPrefs = fPrefs;
- theRef = fVRef;
- if (!Get_Set_Prefs(tempPrefs, theRef , setting)) /* we couldn't save the preferences */
- err = CheckError(fnfErr, noErr, kSaving);
- tempPrefs = fOldPrefs;
- DisposPtr(tempPrefs);
- }
- if (fLoaded == kNotLoaded){
- tempPrefs = fPrefs;
- DisposPtr(tempPrefs);
- }
- if (fAboutPic){
- pic = fAboutPic;
- KillPicture(pic);
- }
- inherited::Close();
- }
-
-
- void PixelFlipper::ItemHit(item)
- int item;
- /* -> nothing is passed. This dialog will handle the users preferences for hotKey,
- displaying the start icon, and making the changes permanent.
- <- nothing is returned.
- */
- { Rect tempRect;
- short DType, theRefNum, itemHit, temp;
- Handle DItem;
- DialogPtr theDlog;
-
- theDlog = dp;
- GetDItem(theDlog, item + lastItem, &DType, &DItem, &tempRect);
- switch (item){
- case kOption:
- Toggle(optionKey, (ControlHandle)DItem);
- break;
-
- case kShift:
- Toggle(shiftKey, (ControlHandle)DItem);
- break;
-
- case kCommand:
- Toggle(cmdKey, (ControlHandle)DItem);
- break;
-
- case kControl:
- Toggle(controlKey, (ControlHandle)DItem);
- break;
-
- case kCapsLock:
- Toggle(alphaLock, (ControlHandle)DItem);
- break;
-
- case kShowIcon:
- temp = GetCtlValue((ControlHandle)DItem);
- SetCtlValue((ControlHandle)DItem, ((temp + 1) & 1));
- fOldPrefs->showIcon = (temp + 1) & 1;
- break;
-
- case kChanges:
- temp = GetCtlValue((ControlHandle)DItem);
- SetCtlValue((ControlHandle)DItem, ((temp + 1) & 1));
- fOldPrefs->isPerm = (temp + 1) & 1;
- break;
-
- case kRevert:
- SetPrefsCtl();
- *fOldPrefs =*fPrefs;
- break;
-
- case kActive:
- SetDCtl(kActive, TRUE);
- SetDCtl(kInactive, FALSE);
- fOldPrefs->active = TRUE;
- break;
-
- case kInactive:
- SetDCtl(kActive, FALSE);
- SetDCtl(kInactive, TRUE);
- fOldPrefs->active = FALSE;
- break;
-
- case kAbout:
- case kCICN:
- fAbout = !fAbout;
-
- }
- }
-
- void PixelFlipper::Idle()
- { long tix;
-
- if (fAbout){
- if (fScrolled > fAmount){
- fScrolled = 0;
- Delay(75, &tix);
- }else
- fScrolled += 1;
- DrawAbout();
- }
- }
-
- void PixelFlipper::DoCursor()
- { Cursor the_Curs;
-
- the_Curs = fCursor;
- SetCursor(&the_Curs);
- }
-
- void PixelFlipper::Toggle(short whichKey, ControlHandle theItem)
- /* -> theKey, theItem. The key is the modifiers flag, and theItem is a
- ControlHandle that we are toggeling on and off. For our purposes
- at least 1 item in the dialog has to be on for us to be active.
- <- nothing is returned.
- */
- { short value;
-
- value = GetCtlValue(theItem);
- SetCtlValue(theItem, ((value + 1) & 1));
- if ((fOldPrefs->hotKey ^= whichKey)==0){ /* have to have at least one selected */
- SetCtlValue(theItem, 1);
- fOldPrefs->hotKey ^= whichKey;
- }
- }
-
- void PixelFlipper::SetDCtl(short which, char value)
- /* -> dlog, which, value. dlog is the dialog on which the controls are stored. which is
- the control to set. value is the state to set the control to.
- <- nothing is returned.
- */
- { Rect theRect;
- short theType;
- Handle theItem;
- DialogPtr theDlog;
-
- theDlog = dp;
- GetDItem(theDlog, which + lastItem, &theType, &theItem, &theRect);
- SetCtlValue((ControlHandle)theItem, value);
- }
-
- void PixelFlipper::DrawAbout()
- { RgnHandle theRgn;
- Handle item;
- Rect bounds, clippedRect;
- PicHandle picH;
- short itemType;
- DialogPtr theDlog;
-
- theDlog = dp;
- if (fClipRgn == NIL)
- return;
- theRgn = fClipRgn;
- GetClip(theRgn);
- GetDItem(theDlog, kB1 + lastItem, &itemType, &item, &bounds);
- ClipRect(&bounds);
- if(fAboutPic){
- picH = fAboutPic;
- clippedRect = (**fAboutPic).picFrame;
- SetRect(&clippedRect, bounds.left, bounds.top, bounds.left + ((**fAboutPic).picFrame.right - (**fAboutPic).picFrame.left), bounds.top + ((**fAboutPic).picFrame.bottom - (**fAboutPic).picFrame.top));
- OffsetRect(&clippedRect, 0, -fScrolled);
- DrawPicture(picH, &clippedRect);
- if (!fAbout)
- ValidRect(&bounds);
- }
- SetClip(theRgn);
- }
-
- void PixelFlipper::Update()
- { short itemType, oldFont, oldSize, bits;
- Handle item;
- Rect bounds, clippedRect;
- Str255 str, str2;
- GrafPtr port;
- GDHandle curGDev;
- Boolean mode;
- DialogPtr theDlog;
- VersRecHdl v;
- CIconHandle cicn;
-
- theDlog = dp;
- GetPort(&port);
- oldFont = port->txFont;
- oldSize = port->txSize;
- TextFont(geneva);
- TextSize(9);
- DrawAbout();
- if (cicn = GetCIcon(kICNGood)){
- GetDItem(theDlog, kCICN + lastItem, &itemType, &item, &bounds);
- HLock (cicn);
- PlotCIcon(&bounds, cicn);
- HUnlock(cicn);
- DisposCIcon(cicn);
- }
- GetDItem(theDlog, kB2 + lastItem, &itemType, &item, &bounds);
- if (fLoaded == kNotLoaded)
- GetIndString(str, kStrErr, kNotLoaded);
- else{
- curGDev = GetGDevice(); /* always update the graphics device global */
- mode = ((**curGDev).gdFlags & 1);
- bits =(**(**curGDev).gdPMap).pixelSize;
- NumToString((long)bits, str);
- GetIndString(str2, kStrPixel, kBits);
- pStrcat(str, str2);
- GetIndString(str2, kStrPixel, kComma);
- pStrcat(str, str2);
- itemType = mode ? kColorS : kGrayS;
- GetIndString(str2, kStrPixel, itemType);
- pStrcat(str, str2);
- if(v = (VersRecHdl)Get1Resource('vers', 1)){
- GetIndString(str2, kStrPixel, kComma);
- pStrcat(str, str2);
- bits = 0;
- switch ((**v).stage){
- case development:
- bits = kDev;
- break;
- case alpha:
- bits = kAlpha;
- break;
- case beta:
- bits = kBeta;
- break;
- case final:
- bits = kfinal;
- break;
- }
- if (bits != 0){
- GetIndString(str2, kStrPixel, bits);
- pStrcat(str, str2);
- pStrcat(str, (**v).message);
- ReleaseResource(v);
- }
- }
- }
- TextBox(&str[1], (long)str[0], &bounds, teJustLeft);
- ValidRect(&bounds);
- TextFont(oldFont);
- TextSize(oldSize);
- SetPort(port);
- }
-
- void PixelFlipper::ClearPrefsCtl()
- {
- SetDCtl(kChanges, FALSE);
- SetDCtl(kShowIcon, FALSE);
- SetDCtl(kControl, FALSE);
- SetDCtl(kOption, FALSE);
- SetDCtl( kCapsLock, FALSE);
- SetDCtl(kShift, FALSE);
- SetDCtl(kCommand, FALSE);
- SetDCtl(kActive, FALSE);
- SetDCtl(kInactive, FALSE);
- }
-
- void PixelFlipper::SetPrefsCtl()
- {
- ClearPrefsCtl();
- if (fPrefs->isPerm)
- SetDCtl(kChanges, fPrefs->isPerm);
- if (fPrefs->showIcon)
- SetDCtl(kShowIcon, fPrefs->showIcon);
- if (fPrefs->hotKey & controlKey) /* controlKey */
- SetDCtl(kControl, 1);
- if (fPrefs->hotKey & optionKey) /* optionKey */
- SetDCtl(kOption, 1);
- if (fPrefs->hotKey & alphaLock) /* cap lock */
- SetDCtl(kCapsLock, 1);
- if (fPrefs->hotKey & shiftKey) /* shiftKey */
- SetDCtl(kShift, 1);
- if (fPrefs->hotKey & cmdKey) /* cmdKey */
- SetDCtl(kCommand, 1);
- if (fPrefs->active) /* is INIT active */
- SetDCtl(kActive, 1);
- else
- SetDCtl(kInactive, 1);
- }
-